package com.huaweicloud.saaslanding.install.applicationservice.impl;

import org.redisson.api.RBatch;
import org.redisson.api.RKeys;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.huaweicloud.saaslanding.install.applicationservice.RedisService;

import lombok.extern.slf4j.Slf4j;

/**
 * 功能描述
 *
 * @since 2022-12-29
 */
@Slf4j
@Component
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void deleteRedis() {
        log.info("========start delete redis========");
        RKeys keys = redissonClient.getKeys();
        Iterable<String> keysByPattern = keys.getKeysByPattern("*", 100);
        RBatch batch = redissonClient.createBatch();
        int count = 0;
        for (String s : keysByPattern) {
            count++;
            batch.getBucket(s).deleteAsync();
            if (count % 100 == 0) {
                batch.execute();
                batch = redissonClient.createBatch();
            }
        }
        if (count % 100 > 0) {
            batch.execute();
        }
        log.info("========end delete redis========count : {}", count);
    }

}
